/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 4, 2013  3:03:57 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.commons
 * File Name : SapTable.java
 * TODO
 */
package sap.business.commons;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;

import sap.jco.client.Connection;
import sun.util.logging.resources.logging;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 4, 2013 : 3:03:57 PM 
 * CreateBy=Chatree Siangklom
 * 
 * SapTable
 * TODO
 */
public class SapTable {
	
	
	private static final Logger logging = LoggerFactory.getLogger(SapTable.class);

	private String sap_table;
	private JCoFunction jCoFunction;
	
	/**
	 * @return the sap_table
	 * Date =Oct 4, 2013 : 3:04:47 PM 
	 * CreateBy=Chatree Siangklom
	 * SapTable.java String
	 * TODO
	 */
	public String getSap_table() {
		return sap_table;
	}
	
	/**
	 * Date =Oct 4, 2013 : 3:03:57 PM 
	 * CreateBy=Chatree Siangklom
	 * SapTable
	 * TODO
	 */
	public SapTable(String sap_table) {
		this.sap_table = sap_table;
	}
	
	public SapTable(String sap_table, JCoFunction jCoFunction) {
		this.jCoFunction = jCoFunction;
		this.sap_table = sap_table;
	}

	/**
	 * Date =Oct 4, 2013 : 5:32:23 PM 
	 * CreateBy=Chatree Siangklom
	 * JCoTable
	 * TODO
	 * @return
	 * @throws JCoException
	 * @throws IOException
	 * @throws Exception
	 */
	public JCoTable getJCoTable()  throws JCoException, IOException, Exception {
		if(jCoFunction == null) 
			throw new Exception("SAP Connection is null !!!");
		
		JCoTable jcoTable = jCoFunction.getTableParameterList().getTable(getSap_table());
		//JCoTable jcoTable = jcoConnection.getTables("IT_CUSTPROFILE");
		
		
		if(jcoTable == null)
			throw new Exception("SAP Table "+ getSap_table() +" is null or isEmpty !!!");
				
		return jcoTable;
	}
	
	/**
	 * 
	 * Date =Jan 15, 2014 : 11:06:52 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param jcoTable
	 */
	public void JcoTableToString(JCoTable jcoTable){
		try{
			logging.info((jcoTable == null)?"jcoTable is null":"jcoTable is not null size=" + jcoTable.getNumRows());
			if(jcoTable != null && jcoTable.getNumRows() > 0){
				
				String tmp = "";
				for(int i = 0; i < jcoTable.getNumRows(); i++){
					jcoTable.setRow(i);
					
					for(int j = 0; j < jcoTable.getNumColumns(); j++){
						if(j == 0)
							tmp += jcoTable.getMetaData().getName(j) + "=" + jcoTable.getString(j);
						else
							tmp += "," + jcoTable.getMetaData().getName(j) + "=" + jcoTable.getString(j);
					}
					
					logging.info("JcoRowNum : " + i + "." + tmp);
				}				
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			logging.error(e.getMessage());
		}
	}
}
